const router = require('koa-router')()
// const indexModel = require('../models/index-model')
const jwt = require('jsonwebtoken')

router.post('/api/user/login', async (ctx, next) => {
  try {
    // let indexData = await indexModel.indexHelloModel()
    // await ctx.render('index.njk', indexData)
    ctx.set('Content-Type', 'application/json')
    const moment = new Date()
    const expireDate = new Date(moment.setDate(moment.getDate() + 1))
    ctx.body = jwt.sign({
      userid: 'userxie',
      username: 'xie',
      email: '471519362@qq.com',
      nickName: 'Sketcher',
      avatar: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1122649470,955539824&fm=27&gp=0.jpg',
      expire: expireDate.toISOString()
    }, 'shhhhh')
  } catch (err) {
    ctx.status = err.status || 500
    ctx.body = err.message
    ctx.app.emit('error', err, ctx)
  }
})

router.post('/api/user/refresh', async (ctx, next) => {
  try {
    // let indexData = await indexModel.indexHelloModel()
    // await ctx.render('index.njk', indexData)
    ctx.set('Content-Type', 'application/json')
    const moment = new Date()
    const expireDate = new Date(moment.setDate(moment.getDate() + 1))
    ctx.body = jwt.sign({
      userid: 'userxie',
      username: 'xie',
      email: '471519362@qq.com',
      nickName: 'Sketcher',
      avatar: 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1122649470,955539824&fm=27&gp=0.jpg',
      expire: expireDate.toISOString()
    }, 'shhhhh')
  } catch (err) {
    ctx.status = err.status || 500
    ctx.body = err.message
    ctx.app.emit('error', err, ctx)
  }
})

module.exports = router
